package com.gaj.util;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WordReader {

    private static List<String> titles = new ArrayList<>();

    public static void main(String[] args) throws IOException {
        // 指定文件路径
        String filePath = "D:\\users\\Desktop\\BaiduSyncdisk\\工作\\外包\\BJCM政法智能办案平台中心平台项目技术接口标准V4.5.4.2\\03-政法智能办案平台中心平台项目业务代码标准v4.5.2.doc";

        if (filePath.endsWith(".docx")) {
            readDocx(filePath);
        } else if (filePath.endsWith(".doc")) {
            readDoc(filePath);
        } else {
            System.out.println("Unsupported file type");
        }
    }

    // 读取 .docx 文件
    private static void readDocx(String filePath) throws IOException {
        try (FileInputStream fis = new FileInputStream(filePath)) {
            XWPFDocument document = new XWPFDocument(fis);

            // 获取所有段落
            List<XWPFParagraph> paragraphs = document.getParagraphs();

            // 遍历段落并查找标题2和标题3
            for (XWPFParagraph paragraph : paragraphs) {
                // 获取段落文本并检查其是否为标题2或标题3
                String paragraphText = getParagraphText(paragraph);
                if (paragraph.getStyle() != null) {
                    String style = paragraph.getStyle();
                    if (style.equals("Heading 2") || style.equals("Heading 3")) {
                        System.out.println(paragraphText);
                    }
                }
            }
        }
    }

    // 获取段落的完整文本，包括编号
    private static String getParagraphText(XWPFParagraph paragraph) {
        StringBuilder paragraphText = new StringBuilder();
        List<XWPFRun> runs = paragraph.getRuns();

        // 遍历段落的所有run，拼接成完整文本
        for (XWPFRun run : runs) {
            paragraphText.append(run.text());
        }

        return paragraphText.toString().trim();
    }

    // 读取 .doc 文件
    private static void readDoc(String filePath) throws IOException {
        try (FileInputStream fis = new FileInputStream(filePath)) {
            HWPFDocument document = new HWPFDocument(fis);
            Range range = document.getRange();

            // 遍历段落并查找标题2和标题3
            for (int i = 0; i < range.numParagraphs(); i++) {
                String paragraphText = range.getParagraph(i).text().trim();

                // 打印标题
                if (isHeading(paragraphText, range.getParagraph(i).getStyleIndex())) {
                    titles.add(paragraphText);
                }
            }

            // 处理并打印带序号的标题
            List<String> numberedTitles = addNumbersToTitles(titles);

            // 输出带序号的标题
            for (String title : numberedTitles) {
                System.out.println(title);
            }

            createFileByTitles(numberedTitles);
        }
    }

    //遍历集合里的内容, 创建文件
    private static void createFileByTitles(List<String> titles) {
        String filePath = "D:\\idea_projects\\gajSpringBoot\\doc\\sql\\字典数据";
        for (String title : titles) {
            File file = new File(filePath + "\\" + title + ".sql");
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 判断是否是标题2或标题3
    private static boolean isHeading(String paragraphText, int styleIndex) {
        // 假设样式索引 2 和 3 对应标题 2 和标题 3
        return styleIndex == 2 || styleIndex == 3;
    }

    // 给标题列表添加序号
    private static List<String> addNumbersToTitles(List<String> titles) {
        List<String> numberedTitles = new ArrayList<>();
        int mainIndex = 2; // 主级序号
        int subIndex = 1;  // 子级序号

        for (int i = 0; i < titles.size(); i++) {
            String title = titles.get(i);

            // 处理每个标题的编号逻辑
            if (i > 0 && (titles.get(i).endsWith("组织机构代码") || titles.get(i).endsWith("组织机构"))) {
                // 增加子级编号
                numberedTitles.add(mainIndex + "." + subIndex + "." + title);
                subIndex++;
            } else {
                // 主标题编号
                numberedTitles.add(mainIndex + "." + title);
                subIndex = 1;  // 重置子级序号
                mainIndex++;   // 主标题递增
            }
        }

        return numberedTitles;
    }
}
